package com.alibaba.citrus.cr.common.util;

import java.util.Objects;

import com.alibaba.citrus.cr.common.exception.ActivityException;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.exception.ProcessException;
import com.alibaba.citrus.cr.common.exception.RepositoryException;
import com.alibaba.cz.base.tool.message.ErrorMessage;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.PropertyKey;

/**
 * @author: qiaojiange
 * @create: 2021/09/16
 * @Description 检查参数工具
 */
public class CheckUtils {
    private static final String ERROR_CODE_BUNDLE = "i18n.errors";

    public static void check(boolean test, ErrorMessage errorMessage, Class clazz) {
        if (!test) {
            buildException(errorMessage, clazz);
        }
    }

    /**
     * 检验逻辑
     *
     * @param test      为 false 抛异常
     * @param errorCode 错误码
     * @param args      错误信息
     */
    public static void check(boolean test, @PropertyKey(resourceBundle = "i18n.errors") String errorCode, Class clazz, Object... args) {
        check(test, ErrorMessage.code(errorCode, args), clazz);
    }

    public static void checkNotNull(Object obj, ErrorMessage errorMessage, Class clazz) {
        if (obj == null) {
            buildException(errorMessage, clazz);
        }
    }

    /**
     * 构建异常
     *
     * @param errorMessage
     * @param clazz
     */
    private static void buildException(ErrorMessage errorMessage, Class clazz) {
        if (FacadeException.class.equals(clazz)) {
            throw new FacadeException(errorMessage);
        } else if (ActivityException.class.equals(clazz)) {
            throw new ActivityException(errorMessage);
        } else if (FunctionException.class.equals(clazz)) {
            throw new FunctionException(errorMessage);
        } else if (ProcessException.class.equals(clazz)) {
            throw new ProcessException(errorMessage);
        } else if (RepositoryException.class.equals(clazz)) {
            throw new RepositoryException(errorMessage);
        }
    }

    /**
     * 构建异常
     *
     * @param clazz
     */
    private static void buildException(String errorCode, Class clazz) {
        if (FacadeException.class.equals(clazz)) {
            throw new FacadeException(errorCode);
        } else if (ActivityException.class.equals(clazz)) {
            throw new ActivityException(errorCode);
        } else if (FunctionException.class.equals(clazz)) {
            throw new FunctionException(errorCode);
        } else if (ProcessException.class.equals(clazz)) {
            throw new ProcessException(errorCode);
        } else if (RepositoryException.class.equals(clazz)) {
            throw new RepositoryException(errorCode);
        }
    }

    public static void checkNotNull(Object obj, @PropertyKey(resourceBundle = "i18n.errors") String errorCode, Class clazz, Object... args) {
        checkNotNull(obj, ErrorMessage.code(errorCode, args), clazz);
    }

    public static void checkNotBlank(String str, ErrorMessage errorMessage, Class clazz) {
        if (StringUtils.isBlank(str)) {
            buildException(errorMessage, clazz);
        }
    }

    public static void checkNotBlank(String str, @PropertyKey(resourceBundle = "i18n.errors") String errorCode, Class clazz, Object... args) {
        checkNotBlank(str, ErrorMessage.code(errorCode, args), clazz);
    }

    public static void checkEquals(Object a, Object b, ErrorMessage errorMessage, Class clazz) {
        if (!Objects.equals(a, b)) {
            buildException(errorMessage, clazz);
        }
    }

    public static void checkEquals(Object a, Object b, @PropertyKey(resourceBundle = "i18n.errors") String errorCode, Class clazz, Object... args) {
        checkEquals(a, b, ErrorMessage.code(errorCode, args), clazz);
    }

    public static void checkEffectOne(int effectNum, @PropertyKey(resourceBundle = "i18n.errors") String errorCode, Class clazz) {
        if (effectNum != 1) {
            buildException(errorCode, clazz);
        }
    }
}
